<?php
//header('Content-Type:text/html;charset=utf-8');
header('Content-Type:application/json;charset=utf-8');
error_reporting(0);
require_once 'Autoload.php';
$service = new Service();


$code=$_REQUEST['code'];
//$id=$_REQUEST['id'];
$callback=$_REQUEST['callback'];


switch($code){
    case "addcontent":
        //搜下数据库然后拼成array
//        $service->savelog(serialize($res));
//        echo $callback."(".json_encode(array("code"=>$code,"price"=>333,"ticket"=>$id)).")";
        $query = "
        insert into fybx(date,zflx,money,content) VALUES ('{$_REQUEST['date']}','{$_REQUEST['zflx']}','{$_REQUEST['money']}','{$_REQUEST['content']}')
        ";
        $res = $service->travel->execute_dml($query);
        $id = $service->travel->get_InsertId();
        echo $callback."(".json_encode(array("res"=>$res,"data"=>array("date"=>$_REQUEST['date'],"zflx"=>$_REQUEST['zflx'],"money"=>$_REQUEST['money'],"content"=>$_REQUEST['content'],"id"=>$id)),JSON_UNESCAPED_UNICODE).")";
        break;
    //deleitem
    case "deleitem":
        //搜下数据库然后拼成array
//        $service->savelog(serialize($res));
//        echo $callback."(".json_encode(array("code"=>$code,"price"=>333,"ticket"=>$id)).")";
        $id = $_REQUEST['id'];
        $query = "
        delete from fybx where id = $id
        ";
        $res = $service->travel->execute_dml($query);
        echo $callback."(".json_encode(array("res"=>$res),JSON_UNESCAPED_UNICODE).")";
        break;


    case "search":
        $query = "SELECT
IFNULL(zflx,'总计')as zflx,
SUM(money) as total
FROM `fybx`
where DATE_FORMAT(date,'%Y-%m-%d')>='{$_REQUEST['begin']}' and DATE_FORMAT(date,'%Y-%m-%d')<='{$_REQUEST['end']}'
GROUP BY zflx
WITH ROLLUP";
        $service->savelog($query);
        $res = $service->travel->execute_dql_assoc($query);
        echo $callback."(".json_encode(array("res"=>$res),JSON_UNESCAPED_UNICODE).")";
        break;

    case "searchdetail":
        $zflx = $_REQUEST['zf']=="总计"?"":"and zflx = '{$_REQUEST['zf']}'";
        $query = "SELECT
*
FROM `fybx`
where DATE_FORMAT(date,'%Y-%m-%d')>='{$_REQUEST['begin']}' and DATE_FORMAT(date,'%Y-%m-%d')<='{$_REQUEST['end']}' $zflx
";
        $res = $service->travel->execute_dql_assoc($query);
        echo $callback."(".json_encode(array("res"=>$res),JSON_UNESCAPED_UNICODE).")";
        break;


    case "editsubmit":
        $id = $_REQUEST['id'];
        $query = "
        update fybx set date='{$_REQUEST['date']}',zflx='{$_REQUEST['zflx']}', content='{$_REQUEST['content']}', money='{$_REQUEST['money']}' where id=$id
        ";
        $res = $service->travel->execute_dml($query);
        echo $callback."(".json_encode(array("res"=>$res),JSON_UNESCAPED_UNICODE).")";
        break;

    default:
        break;
}

//这里用sql查到code的价格和余票
//echo $callback."(".json_encode(array("code"=>$code,"price"=>5555,"ticket"=>5)).")";

die();



//$obj = new stdClass();
$obj = json_decode(file_get_contents('php://input'),false);//转成对象 true是数组
//@$obj->url = "http://www.gsp.com/rest/index.php";//提取到外部 测试戴军电脑
//@$obj->url = "http://test.gsp.chinawayltd.com/rest/index.php";//提取到外部 测试戴军电脑
//@$obj->url = "http://gsp.huoyunren.com/rest/index.php";//提取到外部 测试戴军电脑
$type = @$_REQUEST['type'];
$type($obj);
//$type($service,$obj);
//getPri();
//查询 读取查询(Retrieve)
function r($query){
    global $service;
    print_r(json_encode($service->gstjTool->execute_dql_assoc($query),JSON_UNESCAPED_UNICODE));
}
//CRUD增加(Create)、、更新(Update)和删除(Delete)
function d($query){
    global $service;
    print_r(json_encode($service->gstjTool->execute_dml($query),JSON_UNESCAPED_UNICODE));
}

function o($query){
    global $service;
    echo json_encode($service->gstjTool->execute_dql_one_row($query),JSON_UNESCAPED_UNICODE);
}

//得到调度的省province by sql
function getXmmc($obj){
    $query="select id from user where name = '$obj->name'";
    global $service;
    $id = $service->gstjTool->execute_dql_one_row($query);
    $query="select a.name as value,a.name,a.`code`
from project as a
left join user_pro as b on a.id=b.pro_id
where b.user_id = $id";
//    print_r(json_encode($service->gspTool->execute_dql_assoc($query)));//正式环境
//    print_r(json_encode($service->gspTool->execute_dql_assoc($query)));
    r($query);
}

function sendGstj($obj){
    global $service;

    try{
        if($obj->hour==0){
            throw new Exception('小时数不能为0',1001);
        }
        $week = getWeek($obj->date);
        $day = getDay($obj->date);
        $time = time();
        $query = "insert into gstj(name,xmmc,xmdm,week,beizhu,$day) VALUES ('$obj->name','$obj->xmmc','$obj->code','$week','$obj->beizhu','$obj->hour')";
//        d($query);
//        global $service;
        $service->gstjTool->execute_dml($query);
        $gstjid = $service->gstjTool->get_InsertId();
//        echo $gstjid;
        $query = "insert into pmis_gstj(name,xmmc,xmdm,week,beizhu,date,hour,updatetime,gstjid) VALUES ('$obj->name','$obj->xmmc','$obj->code','$week','$obj->beizhu','$obj->date','$obj->hour',$time,$gstjid)";
        $res = $service->gstjTool->execute_dml($query);
        if($res==1){
            throw new Exception('填写成功!',1000);
        }else{
            throw new Exception('填写失败!',1003);
        }
//        d($query);
    }catch(Exception $e){
//        print_r(json_encode(['code'=>$e->getCode(),'msg'=>$e->getMessage()],JSON_UNESCAPED_UNICODE));
        restful($e->getCode(),$e->getMessage());
    }

}

function restful($code,$msg){
    print_r(json_encode(['code'=>$code,'msg'=>$msg],JSON_UNESCAPED_UNICODE));
}

function getReportByDate($obj){
    $query = "SELECT xmmc as name,sum(hour) as value
FROM `pmis_gstj`
WHERE date='$obj->today' and name='$obj->name' and hour>0
GROUP BY name,xmmc";
    r($query);
}


function delGs($obj){
    global $service;
    $query1 = "SELECT id,date,xmmc,hour
FROM `pmis_gstj`
WHERE name='$obj->name'
and date is not null
ORDER BY date desc
limit ".($obj->currentPage-1)*$obj->pageSize.",". $obj->pageSize;
    $query2 = "SELECT count(*)
FROM `pmis_gstj`
WHERE name='$obj->name'
and date is not null";
    $arr = [
        'data'=>$service->gstjTool->execute_dql_assoc($query1),
        'total'=>$service->gstjTool->execute_dql_one_row($query2),
    ];
//    echo $query;
//    r($query);
    print_r(json_encode($arr,JSON_UNESCAPED_UNICODE));
}

function delitem($obj){
    global $service;
    $query = "select gstjid from pmis_gstj where id = $obj->id";
    $gstjid = $service->gstjTool->execute_dql_one_row($query);
    $query = "delete from gstj where id = $gstjid";
    d($query);
    $query = "delete from pmis_gstj where id = $obj->id";
    d($query);
}

//得到周
function getWeek($date){//从这里控制周,从第2017年从53周开始
    $day=date('w',strtotime($date));
    $year=date('Y',strtotime($date));
    if ($day==0) { //周日
//		if ($year==2017) {
//			return date('W',strtotime($date))+1+52;
//		}
//		elseif($year==2018){
//			return date('W',strtotime($date))+1+104;
//		}
//		else{
//			return date('W',strtotime($date))+1;
//		}
        switch ($year){
            case 2016:return date('W',strtotime($date))+1;break;
            case 2017:
                if($date=='2017-01-01'){
                    return date('W',strtotime($date))+1;break;
                }else{
                    return date('W',strtotime($date))+1+52;break;
                }
            case 2018:return date('W',strtotime($date))+1+104;break;
            default:break;
        }

    }else{
//		if ($year==2017) {
//			return date('W',strtotime($date))+52;
//		}
//        elseif($year==2018){
//			return date('W',strtotime($date))+104;
//		}
//        else{
//			return date('W',strtotime($date));
//		}

        switch ($year){
            case 2016:return date('W',strtotime($date));break;
            case 2017:return date('W',strtotime($date))+52;break;
            case 2018:return date('W',strtotime($date))+104;break;
            default:break;
        }
    }
}
/*
 *写给良本上传csv用的函数
 */
function getDay($data){
    $day=date('w',strtotime($data));
    switch($day){
        case 0: return "sun";break;
        case 1: return "mon";break;
        case 2: return "tue";break;
        case 3: return "wed";break;
        case 4: return "thu";break;
        case 5: return "fri";break;
        case 6: return "sat";break;
    }
}


function findNewName($obj){
    $query = "SELECT * FROM `user` where name  like '$obj->oldname%' and name!='$obj->oldname'";
    r($query);
}

function getOldUser($obj){
    $query = "SELECT * FROM `user` where name='$obj->oldname'";
    r($query);
}

function fixUser($obj){//olduser newuser

//    {"hour":0,"status":0,"oldname":"张晓辉",
//        "olduser":{"id":"412","name":"张晓辉","pwd":"123","area":"企业方案事业部","pid":"张晓辉","role":"3","email":"华北","phone":""},
//        "newname":"张晓辉_企业产品事业部",
//        "newuser":{"id":"1238","name":"张晓辉_企业产品事业部","pwd":"123","area":"综合管理部","pid":"1","role":"1","email":null,"phone":null}}


//    echo $obj->olduser->area;
    //1.修复信息
    $query = "update user set area='{$obj->olduser->area}',pid='{$obj->olduser->pid}',role='{$obj->olduser->role}',email='{$obj->olduser->email}',phone='{$obj->olduser->phone}' where id='{$obj->newuser->id}'";
//    echo $query;
    d($query);

    //2.修复工时
    $query = "UPDATE gstj set name = '{$obj->newuser->name}' where name ='{$obj->olduser->name}'";
//    echo $query;
    d($query);

    //3.修复选的项目
    $query = "UPDATE user_pro set user_id ='{$obj->newuser->id}' where user_id='{$obj->olduser->id}'";
    d($query);

    //4.修复pm
    $query = "UPDATE pm_pro set pm='{$obj->newuser->name}' where pm ='{$obj->olduser->name}'";

    d($query);

}
